home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: johnb@pivotal-dm.ccmail.compuserve.com (John Bain)
- Newsgroups: comp.lang.c,comp.lang.c.moderated
- Subject: Integral promotion problem
- Date: 8 Mar 1996 17:04:26 -0600
- Organization: Pivotal Technologies Ltd.
- Sender: clc@solutions.solon.com
- Approved: clc@solutions.solon.com
- Message-ID: <4hqedq$1mj@solutions.solon.com>
- NNTP-Posting-Host: solutions.solon.com
- X-Newsreader: Forte Agent .99d/32.182
-
- Hi,
-
- I thought I understood integral promotion, but the following example has
- me puzzled.
-
- --------------------
- #include <stdio.h>
-
- unsigned short s = 0xFFFF;
-
- int main(void)
- {
- int i = ~s;
-
- printf ("%x %x\n", i, (int)~s);
-
- if (i)
- printf("i - NonZero\n");
- else
- printf("i - Zero\n");
-
- if ((int)~s)
- printf("(int)~s - NonZero\n");
- else
- printf("(int)~s - Zero\n");
-
- return 0;
- }
- --------------------
-
- When compiled on an implementation with 32-bit 2's complement ints I get
- the following output:
-
- -------------------
- ffff0000 ffff0000
- i - NonZero
- (int)~s - Zero
- ------------------
-
- The hex values and the result of the first "if" are the "surprising"
- results I was expecting. However, the result of the second "if" has me
- confused.
-
- What am I missing?
-
- (In case it's relevent, the implementation defines the conversion of an
- unsigned integer to a signed integer, when the unsigned value cannot be
- represented in the signed integer, as being performed by truncation of
- the most significant bits if the signed integer is shorter, and then
- interpreting the remaining bits as a 2's complement integer.)
-
- Cheers,
-
- John
-
- -----------------------------------------------------------------
- John Bain
- johnb@pivotal-dm.ccmail.compuserve.com
- -----------------------------------------------------------------
-